<?php
    $item_menu[0] = 4; 
    $item_menu[1] = 2;  
    $title = 'Usuarios';
	include('header.php');

	if (!isset($_SESSION["admin"])) {redirect("login.php");	exit(); }

    $titulo = 'Administracion de Usuarios';    
    $letra  = request('letra','A');
    $filtro = request('filtro',''); 
    $id     = request('id',0);
    $accion = request('accion','');

	if(!empty($_SESSION['Msg'])) {
		echo '<center><div style="width:800px;height:30px;background-color:lightblue;border:2px solid blue;font-size:16px;font-weight;bold;text-align:center;margin-top:5px;padding-top:5px;color:blue;">';
		echo $_SESSION['Msg']."</div></center>";
		$_SESSION['Msg']='';
	}
    
	switch ($accion) {

		case 'estado': /******************************************************************************** ESTADO *********************/
			// Cambio el estado: activo/inactivo
			$cond = "select * from usuarios where id=".$id;
			$rs   = $db->SelectLimit($cond, 1);
			$u	  = $rs->FetchRow();
			if ( $u['activo']==1 ) { $u['activo']=0; } else { $u['activo']=1; }
			$cond 	= "update usuarios set activo='{$u['activo']}' where id='$id' ";
			$ok		= $db->Execute($cond);
			break;

		case 'eliminar': /******************************************************************************** DELETE *********************/
			$cond 	= "delete from usuarios where id='$id'";
			$ok		= $db->Execute($cond);

			if ($ok) { echo mensaje_ok("El Usuario se elimin&oacute; correctamente.");
			} else { echo mensaje_error("ERROR!! No se pudo eliminar el Usuario."); echo $sql; }
			$id = 0;
			break;


	}


	if( empty($filtro) ){
		$sql = "select * from usuarios where LEFT(apellido,1) = '$letra' order  by apellido ASC, nombre ASC";	
	} else {
		$sql = "select * from usuarios 
				where apellido LIKE '%$filtro%' or nombre LIKE '%$filtro%' or email LIKE '%$filtro%'
				order  by apellido ASC, nombre ASC";
	}
	
	$rs = $db->Execute($sql);
	$Usuarios = $rs->GetRows();

?>
<link href="<?php echo ADMIN;?>css_admin.css" rel="stylesheet" type="text/css"/>
<table>
	<tr>
		<td><h2>Listado de Usuarios Registrados</h2></td>
		<td>
			<form action='usuarios.php' method="get">
				<input type='text' name='filtro' value='<?php echo $filtro;?>'/>
				<input type="submit" value="Filtrar / Buscar"/>
			</form>
		</td>
	</tr>
</table>

<table>
    <tr>
        <td width="100" valign='top'> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='a'/><input type="submit" value="A"/></form>
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='b'/><input type="submit" value="B"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='c'/><input type="submit" value="C"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='d'/><input type="submit" value="D"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='e'/><input type="submit" value="E"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='f'/><input type="submit" value="F"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='g'/><input type="submit" value="G"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='h'/><input type="submit" value="H"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='i'/><input type="submit" value="I"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='j'/><input type="submit" value="J"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='k'/><input type="submit" value="K"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='l'/><input type="submit" value="L"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='m'/><input type="submit" value="M"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='n'/><input type="submit" value="N"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='ñ'/><input type="submit" value="Ñ"/></form>  
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='o'/><input type="submit" value="O"/></form>
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='p'/><input type="submit" value="P"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='q'/><input type="submit" value="Q"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='r'/><input type="submit" value="R"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='s'/><input type="submit" value="S"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='t'/><input type="submit" value="T"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='u'/><input type="submit" value="U"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='v'/><input type="submit" value="V"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='w'/><input type="submit" value="W"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='x'/><input type="submit" value="X"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='y'/><input type="submit" value="Y"/></form> 
            <form action='usuarios.php' method="get"><input type='hidden' name='letra' value='z'/><input type="submit" value="Z"/></form> 
        </td>

        <td  valign='top'>

			<table width='700' border='0' cellpadding='4' cellspacing='0'>
				<tr>
					<th width='40'  class='th'>Acción</th>
					<th width='30'  class='th'>Apellido</th>
					<th align='center'  class='th'>Nombre</th>
		    		<th width='100'  class='th'>Email</th>
				</tr>
		<?php 
			foreach($Usuarios as $c) { 
					if ($color=="#FFFFFF") {$color="#FEFFCC"; } else {$color="#FFFFFF";}
		?>
					<tr bgcolor="<?php echo $color;?>">
						<td align='left' nowrap='nowrap'>
							<a href='usuarios.php?accion=estado&id=<?php echo $c['id'];?>&letra=<?php echo $letra;?>&filtro=<?php echo $filtro;?>' title='Activar/Desactivar este Registro'>
								<?php $img = iif ($c['activo']=='1','img/activo.gif','img/inactivo.gif'); ?>
								<img src='<?php echo $img;?>' border='0'  style="margin-top:7px;"></a>
								&nbsp;&nbsp;&nbsp;

							<a href='#' title='Editar este Registro'
							onclick="TINY.box.show({iframe:'<?php echo URL;?>/admin/usuarios_editar.php?id=<?php echo $c['id'];?>',boxid:'frameless',width:700,height:550,fixed:false,maskid:'bluemask',maskopacity:40,closejs:function(){closeJS()}})"	
							><img src='img/edit.gif' border='0' style="margin-top:7px;"></a>

							<?php if($c['user_active']==0){ ?>
								&nbsp;&nbsp;&nbsp;
								<a href='usuarios.php?accion=eliminar&id=<?php echo $c['id'];?>&letra=<?php echo $letra;?>&filtro=<?php echo $filtro;?>' title='Eliminar este Registro'
									onclick="return confirm('Est&aacute; seguro de eliminar este Registro?');">
									<img src='img/del.gif' border='0' ></a>
							<?php } ?>
								
						</td>
						<td ><?php echo $c['apellido'];?></td>
						<td ><?php echo $c['nombre'];?></td>
						<td ><?php echo $c['email'];?></td>
					</tr>


		<?php } // foreach?>
			</table>
		</td>
	</tr>
</table>

<script>
	function closeJS(){
		location.replace("<?php echo $_SERVER['REQUEST_URI'];?>");	
	}
</script>    

</body></html>